package com.xingxue.shopping.web.controllers;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xingxue.shopping.model.entity.Pager;
import com.xingxue.shopping.model.persist.Goods;
import com.xingxue.shopping.model.service.IGoodsService;
import com.xingxue.shopping.model.service.impl.GoodsServiceImpl;

/**
 * Servlet implementation class AddControllers
 */
@WebServlet("/add")
public class AddControllers extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddControllers() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//doGet(request, response);
		Goods g=new Goods();
		g.setGname(request.getParameter("uname"));
		g.setGdesc(request.getParameter("de"));
		g.setGtypeid(Integer.valueOf(request.getParameter("sex")));
		g.setGprice(request.getParameter("up"));
		g.setGcount(Integer.valueOf(request.getParameter("amount")));
		g.setGimage("");
		g.setGsize(request.getParameter("desc"));
		
		IGoodsService service=new GoodsServiceImpl();
		
		int result=service.addGoods(g);
		if (result>0)
		{
			Pager pager=new Pager();
			pager.setTablename("person");
			pager.setFields("*");
			pager.setPagesize(5);
			pager.setPageindex(1);
			pager.setWheres("");
			pager.setOrders("");
			pager.setTotalnum(0);
			//List<Person>list= service.getAll();
			
			List<Goods> list= service.getDataByPage(pager);
			
			int pNum=pager.getTotalnum()%pager.getPagesize()==0?
					 pager.getTotalnum()/pager.getPagesize():
					 pager.getTotalnum()/pager.getPagesize()+1;
					 pager.setPagenum(pNum);
					
			
			request.setAttribute("plist", list);
			request.setAttribute("pager", pager);
			request.getRequestDispatcher("index.jsp").forward(request, response);
		}else 
		{
		 request.setAttribute("msg", "添加失败");	
		 request.getRequestDispatcher("add.jsp").forward(request, response);
		}
		
		
		
		
	}	
	}